# coding=utf-8
import logging
import sys

from center.config import ZOOKEEPER_SERVER
from center.web import run_server
from common.election import EletionManager
from common.zookeeper_client import ZookeeperClient


# from common.utils import get_host_ip


def main():
    logging.basicConfig(level=logging.DEBUG)
    logging.info("Start run center process")

    # 获取本地IP地址
    # local_ip = get_host_ip(IFC_NAME)

    # 连接Zookeeper服务
    zkclient = ZookeeperClient(ZOOKEEPER_SERVER)

    # 开始选举
    manager = EletionManager(zkclient)
    # 阻塞直到成为leader
    manager.do_elect("/election/center", "100")

    # 成为master后，运行web服务器
    # 运行API服务
    port = 5000
    if len(sys.argv) > 1:
        port = sys.argv[1]
    run_server.run_webserver(port)
    logging.info("Finish center process")


if __name__ == "__main__":
    main()
